//
//  HWGameDetailTableProtocol.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/3/10.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWGameDetailTableProtocol: NSObject {
    var sectionTitles = ["游戏简介","游戏福利","玩家评论","猜你喜欢"]
    var tempCell:HWGameDetailBannerPagerVideoCell!
    var scrollDidScroll:((_:UIScrollView)->())?
    var model:HWGameDetailInfoModel?
    var zanButtonClick:((_:String)->())?
    /// 播放
    var gameImageViewClick:((_:UIImageView,_:HWGameDetailInfoModel)->())?
    /// 结束播放
    var endPlay:(()->())?
    
    var jumpToGameDetail:((_:String)->())?
    
    var jumpToCommentDetail:((_:String)->())?
    
    var allButtonClick:(()->())?
    
    var videoPlayClick:((_:String)->())?
}

extension HWGameDetailTableProtocol: UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let arr = model?.commentList,arr.count > 0, indexPath.section == 3 {
            jumpToCommentDetail?(arr[0].id)
        }
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return sectionTitles.count + 1
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = HWGameDetailHeader.headerOrFooterWithTableView(tableView: tableView)
        header.titleLabel.text = sectionTitles[section - 1]
        return header
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
            return 0
        }else {
            return 22*widthScale
        }
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            if (model?.imgs.video.video_url.count ?? 0 > 0) || (model?.imgs.imgs.count ?? 0) > 0 {
                return 1
            }
        }else if section == 1 {
            if (model?.content.count ?? 0) > 0{
                return 1
            }
        }else if section == 2 {
            return 1
        }else if section == 3 {
            if (model?.commentList.count ?? 0) > 0 {
                return 1
            } else {
                return 1
            }
        }else if section == 4 {
            if (model?.cnlike.count ?? 0) > 0 {
                return 1
            }
        }
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let bannerCell = HWGameDetailBannerCell.cellWithTableView(tableView: tableView)
        let introductionCell = HWGameDetailIntroductionCell.cellWithTableView(tableView: tableView)
        let welfareCell = HWGameDetailWelfareCell.cellWithTableView(tableView: tableView)
        let commentCell = HWGameDetailCommentCell.cellWithTableView(tableView: tableView)
        let noDataCell = HWCommentListNoDataCell.cellWithTableView(tableView)
        let likeCell = HWGameDetailLikeCell.cellWithTableView(tableView: tableView)
        if indexPath.section == 0 {
            bannerCell.model = model
            bannerCell.gameImageViewClick = {
                self.gameImageViewClick?($0,$1)
                self.tempCell = $2
            }
            bannerCell.endPlay = {
                self.endPlay?()
            }
            return bannerCell
        } else if indexPath.section == 1 {
            introductionCell.model = model
            introductionCell.openButtonClick = {
                tableView.reloadData()
            }
            return introductionCell
        } else if indexPath.section == 2 {
            welfareCell.model = model
            return welfareCell
        } else if indexPath.section == 3 {
            if (model?.commentList.count ?? 0) > 0 {
                commentCell.model = model?.commentList[indexPath.row]
                commentCell.zanButtonClick = {
                    self.zanButtonClick?($0)
                }
                commentCell.allButtonClick = {
                    self.allButtonClick?()
                }
                commentCell.videoPlayClick = {
                    self.videoPlayClick?($0)
                }
                return commentCell
            } else {
                return noDataCell
            }
        }else if indexPath.section == 4 {
            likeCell.model = model
            likeCell.jumpToGameDetail = {
                self.jumpToGameDetail?($0)
            }
            return likeCell
        }
        return UITableViewCell()
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        self.scrollDidScroll?(scrollView)
    }
    
//    func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
//        if self.tempCell == cell {
//            self.endPlay?()
//            self.tempCell = nil
//        }
//    }
}
